#include <iostream>
#include <sw/redis++/redis++.h>
#include <vector>
#include "util.hpp"
using sw::redis::Redis;

void test1(Redis& redis)
{
    std::cout << "lpush 和 lrange" << std::endl;
    redis.flushall();

    redis.lpush("key","1");
    redis.lpush("key","2");
    redis.lpush("key","3");
    std::vector<std::string> v1 = {"4","0","6"};
    redis.lpush("key",v1.begin(),v1.end());
    std::vector<std::string> v2;
    auto it = std::back_inserter(v2);
    redis.lrange("key",0,-1,it);
    PrintContainer(v2);
}
void test2(Redis& redis)
{
    std::cout << "rpush" << std::endl;
    redis.flushall();

    redis.rpush("key","1");
    redis.rpush("key","2");
    redis.rpush("key","3");
    std::vector<std::string> v1 = {"4","0","6"};
    redis.rpush("key",v1.begin(),v1.end());
    std::vector<std::string> v2;
    auto it = std::back_inserter(v2);
    redis.lrange("key",0,-1,it);
    PrintContainer(v2);
}
void test3(Redis& redis)
{
    std::cout << "lpop 和 rpop" << std::endl;
    redis.flushall();

    redis.lpush("key",{"1","2","3","4"});

    auto result = redis.lpop("key");
    if (result) 
    {
        std::cout << "lpop: " << result.value() << std::endl;
    }

    result = redis.rpop("key");
    if (result) {
        std::cout << "rpop: " << result.value() << std::endl;
    }
}
void test4(Redis& redis)
{
    using namespace std::chrono_literals;
    std::cout << "blpop" << std::endl;
    redis.flushall();

    auto result = redis.blpop("key");
    if (result) 
    {
        std::cout << "key:" << result->first << std::endl;
        std::cout << "elem:" << result->second << std::endl;
    } 
    else 
    {
        std::cout << "result 无效!" << std::endl;
    }
}
void test5(Redis& redis)
{
    std::cout << "llen" << std::endl;
    redis.flushall();
    redis.lpush("key",{"1","2","3"});
    auto len = redis.llen("key");
    std::cout << "len: " << len << std::endl;
}
int main()
{
    Redis redis("tcp://127.0.0.1:6379");
    //test1(redis);
    //test2(redis);
    //test3(redis);
    //test4(redis);
    test5(redis);
}